home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / tex / tools / dvi_uni / macros.zoo / epsf.sty < prev    next >
Encoding:
Text File  |  1992-07-28  |  8.2 KB  |  213 lines

  1. %   EPSF.TEX macro file:
  2. %   Written by Tomas Rokicki of Radical Eye Software, 29 Mar 1989.
  3. %   Revised by Don Knuth, 3 Jan 1990.
  4. %   Revised by Tomas Rokicki to accept bounding boxes with no
  5. %      space after the colon, 18 Jul 1990.
  6. %
  7. %   TeX macros to include an Encapsulated PostScript graphic.
  8. %   Works by finding the bounding box comment,
  9. %   calculating the correct scale values, and inserting a vbox
  10. %   of the appropriate size at the current position in the TeX document.
  11. %
  12. %   To use with the center environment of LaTeX, preface the \epsffile
  13. %   call with a \leavevmode.  (LaTeX should probably supply this itself
  14. %   for the center environment.)
  15. %
  16. %   To use, simply say
  17. %   \input epsf           % somewhere early on in your TeX file
  18. %   \epsfbox{filename.ps} % where you want to insert a vbox for a figure
  19. %
  20. %   Alternatively, you can type
  21. %
  22. %   \epsfbox[0 0 30 50]{filename.ps} % to supply your own BB
  23. %
  24. %   which will not read in the file, and will instead use the bounding
  25. %   box you specify.
  26. %
  27. %   The effect will be to typeset the figure as a TeX box, at the
  28. %   point of your \epsfbox command. By default, the graphic will have its
  29. %   `natural' width (namely the width of its bounding box, as described
  30. %   in filename.ps). The TeX box will have depth zero.
  31. %
  32. %   You can enlarge or reduce the figure by saying
  33. %     \epsfxsize=<dimen> \epsfbox{filename.ps}
  34. %   (or
  35. %     \epsfysize=<dimen> \epsfbox{filename.ps})
  36. %   instead. Then the width of the TeX box will be \epsfxsize and its
  37. %   height will be scaled proportionately (or the height will be
  38. %   \epsfysize and its width will be scaled proportiontally).  The
  39. %   width (and height) is restored to zero after each use.
  40. %
  41. %   A more general facility for sizing is available by defining the
  42. %   \epsfsize macro.    Normally you can redefine this macro
  43. %   to do almost anything.  The first parameter is the natural x size of
  44. %   the PostScript graphic, the second parameter is the natural y size
  45. %   of the PostScript graphic.  It must return the xsize to use, or 0 if
  46. %   natural scaling is to be used.  Common uses include:
  47. %
  48. %      \epsfxsize  % just leave the old value alone
  49. %      0pt         % use the natural sizes
  50. %      #1          % use the natural sizes
  51. %      \hsize      % scale to full width
  52. %      0.5#1       % scale to 50% of natural size
  53. %      \ifnum#1>\hsize\hsize\else#1\fi  % smaller of natural, hsize
  54. %
  55. %   If you want TeX to report the size of the figure (as a message
  56. %   on your terminal when it processes each figure), say `\epsfverbosetrue'.
  57. %
  58. \newread\epsffilein    % file to \read
  59. \newif\ifepsffileok    % continue looking for the bounding box?
  60. \newif\ifepsfbbfound   % success?
  61. \newif\ifepsfverbose   % report what you're making?
  62. \newdimen\epsfxsize    % horizontal size after scaling
  63. \newdimen\epsfysize    % vertical size after scaling
  64. \newdimen\epsftsize    % horizontal size before scaling
  65. \newdimen\epsfrsize    % vertical size before scaling
  66. \newdimen\epsftmp      % register for arithmetic manipulation
  67. \newdimen\pspoints     % conversion factor
  68. %
  69. \pspoints=1bp          % Adobe points are `big'
  70. \epsfxsize=0pt         % Default value, means `use natural size'
  71. \epsfysize=0pt         % ditto
  72. %
  73. \def\epsfbox#1{\global\def\epsfllx{72}\global\def\epsflly{72}%
  74.    \global\def\epsfurx{540}\global\def\epsfury{720}%
  75.    \def\lbracket{[}\def\testit{#1}\ifx\testit\lbracket
  76.    \let\next=\epsfgetlitbb\else\let\next=\epsfnormal\fi\next{#1}}%
  77. %
  78. \def\epsfgetlitbb#1#2 #3 #4 #5]#6{\epsfgrab #2 #3 #4 #5 .\\%
  79.    \epsfsetgraph{#6}}%
  80. %
  81. \def\epsfnormal#1{\epsfgetbb{#1}\epsfsetgraph{#1}}%
  82. %
  83. \def\epsfgetbb#1{%
  84. %
  85. %   The first thing we need to do is to open the
  86. %   PostScript file, if possible.
  87. %
  88. \openin\epsffilein=#1
  89. \ifeof\epsffilein\errmessage{I couldn't open #1, will ignore it}\else
  90. %
  91. %   Okay, we got it. Now we'll scan lines until we find one that doesn't
  92. %   start with %. We're looking for the bounding box comment.
  93. %
  94.    {\epsffileoktrue \chardef\other=12
  95.     \def\do##1{\catcode`##1=\other}\dospecials \catcode`\ =10
  96.     \loop
  97.        \read\epsffilein to \epsffileline
  98.        \ifeof\epsffilein\epsffileokfalse\else
  99. %
  100. %   We check to see if the first character is a % sign;
  101. %   if not, we stop reading (unless the line was entirely blank);
  102. %   if so, we look further and stop only if the line begins with
  103. %   `%%BoundingBox:'.
  104. %
  105.           \expandafter\epsfaux\epsffileline:. \\%
  106.        \fi
  107.    \ifepsffileok\repeat
  108.    \ifepsfbbfound\else
  109.     \ifepsfverbose\message{No bounding box comment in #1; using defaults}\fi\fi
  110.    }\closein\epsffilein\fi}%
  111. %
  112. %   Now we have to calculate the scale and offset values to use.
  113. %   First we compute the natural sizes.
  114. %
  115. \def\epsfclipstring{}% do we clip or not?  If so,
  116. \def\epsfclipon{\def\epsfclipstring{ clip}}%
  117. \def\epsfclipoff{\def\epsfclipstring{}}%
  118. %
  119. \def\epsfsetgraph#1{%
  120.    \epsfrsize=\epsfury\pspoints
  121.    \advance\epsfrsize by-\epsflly\pspoints
  122.    \epsftsize=\epsfurx\pspoints
  123.    \advance\epsftsize by-\epsfllx\pspoints
  124. %
  125. %   If `epsfxsize' is 0, we default to the natural size of the picture.
  126. %   Otherwise we scale the graph to be \epsfxsize wide.
  127. %
  128.    \epsfxsize\epsfsize\epsftsize\epsfrsize
  129.    \ifnum\epsfxsize=0 \ifnum\epsfysize=0
  130.       \epsfxsize=\epsftsize \epsfysize=\epsfrsize
  131.       \epsfrsize=0pt
  132. %
  133. %   We have a sticky problem here:  TeX doesn't do floating point arithmetic!
  134. %   Our goal is to compute y = rx/t. The following loop does this reasonably
  135. %   fast, with an error of at most about 16 sp (about 1/4000 pt).
  136.      \else\epsftmp=\epsftsize \divide\epsftmp\epsfrsize
  137.        \epsfxsize=\epsfysize \multiply\epsfxsize\epsftmp
  138.        \multiply\epsftmp\epsfrsize \advance\epsftsize-\epsftmp
  139.        \epsftmp=\epsfysize
  140.        \loop \advance\epsftsize\epsftsize \divide\epsftmp 2
  141.        \ifnum\epsftmp>0
  142.           \ifnum\epsftsize<\epsfrsize\else
  143.              \advance\epsftsize-\epsfrsize \advance\epsfxsize\epsftmp \fi
  144.        \repeat
  145.        \epsfrsize=0pt
  146.      \fi
  147.    \else \ifnum\epsfysize=0
  148.      \epsftmp=\epsfrsize \divide\epsftmp\epsftsize
  149.      \epsfysize=\epsfxsize \multiply\epsfysize\epsftmp   
  150.      \multiply\epsftmp\epsftsize \advance\epsfrsize-\epsftmp
  151.      \epsftmp=\epsfxsize
  152.      \loop \advance\epsfrsize\epsfrsize \divide\epsftmp 2
  153.      \ifnum\epsftmp>0
  154.         \ifnum\epsfrsize<\epsftsize\else
  155.            \advance\epsfrsize-\epsftsize \advance\epsfysize\epsftmp \fi
  156.      \repeat
  157.      \epsfrsize=0pt
  158.     \else
  159.      \epsfrsize=\epsfysize
  160.     \fi
  161.    \fi
  162. %
  163. %  Finally, we make the vbox and stick in a \special that dvips can parse.
  164. %
  165.    \ifepsfverbose\message{#1: width=\the\epsfxsize, height=\the\epsfysize}\fi
  166.    \epsftmp=10\epsfxsize \divide\epsftmp\pspoints
  167.    \vbox to\epsfysize{\vfil\hbox to\epsfxsize{%
  168.       \ifnum\epsfrsize=0\relax
  169.         \special{PSfile=#1 llx=\epsfllx\space lly=\epsflly\space
  170.             urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp
  171.             \epsfclipstring}%
  172.       \else
  173.         \epsfrsize=10\epsfysize \divide\epsfrsize\pspoints
  174.         \special{PSfile=#1 llx=\epsfllx\space lly=\epsflly\space
  175.             urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp\space
  176.             rhi=\number\epsfrsize \epsfclipstring}%
  177.       \fi
  178.       \hfil}}%
  179. \global\epsfxsize=0pt\global\epsfysize=0pt}%
  180. %
  181. %   We still need to define the tricky \epsfaux macro. This requires
  182. %   a couple of magic constants for comparison purposes.
  183. %
  184. {\catcode`\%=12 \global\let\epsfpercent=%\global\def\epsfbblit{%BoundingBox}}%
  185. %
  186. %   So we're ready to check for `%BoundingBox:' and to grab the
  187. %   values if they are found.
  188. %
  189. \long\def\epsfaux#1#2:#3\\{\ifx#1\epsfpercent
  190.    \def\testit{#2}\ifx\testit\epsfbblit
  191.       \epsfgrab #3 . . . \\%
  192.       \epsffileokfalse
  193.       \global\epsfbbfoundtrue
  194.    \fi\else\ifx#1\par\else\epsffileokfalse\fi\fi}%
  195. %
  196. %   Here we grab the values and stuff them in the appropriate definitions.
  197. %
  198. \def\epsfempty{}%
  199. \def\epsfgrab #1 #2 #3 #4 #5\\{%
  200. \global\def\epsfllx{#1}\ifx\epsfllx\epsfempty
  201.       \epsfgrab #2 #3 #4 #5 .\\\else
  202.    \global\def\epsflly{#2}%
  203.    \global\def\epsfurx{#3}\global\def\epsfury{#4}\fi}%
  204. %
  205. %   We default the epsfsize macro.
  206. %
  207. \def\epsfsize#1#2{\epsfxsize}
  208. %
  209. %   Finally, another definition for compatibility with older macros.
  210. %
  211. \let\epsffile=\epsfbox
  212. ə